home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12582 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: news.sprintlink.net!datalytics!usenet
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: pointer questions
  5. Date: Wed, 20 Mar 1996 11:04:17 -0500
  6. Organization: Datalytics, Inc
  7. Message-ID: <31502C81.1AB6@datalytics.com>
  8. References: <4ifatf$5a8u@uvaix3e1.comp.UVic.CA> <4injoe$qcp@druid.borland.com>
  9. NNTP-Posting-Host: 204.62.224.71
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Pete Becker wrote:
  16. > In article <4ifatf$5a8u@uvaix3e1.comp.UVic.CA>, cgesy@uvaix.uvic.ca says...
  17. > >
  18. > >What does the following statement do/mean? :
  19. > >
  20. > >     ((searchItem&) *this)._refCount++;   //searchItem is the class name
  21. > It tells the compiler to pretend that the 'this' pointer actually points to an
  22. > object of type searchItem, and to increment the member of that object named
  23. > _refCount.
  24.  
  25. More to the point, dereferencing this (*this in the example) 
  26. results in an object of whatever type this points to.  That 
  27. object is then cast to be a reference to class searchItem.  This 
  28. cast is only permissible if this points to a class for which a 
  29. conversion to searchItem* is available (ARM 5.4).  Note that 
  30. constructors and conversion functions are not called as part of 
  31. this conversion.
  32.  
  33. The advantage of this cast to reference is that the result is an 
  34. lvalue.  That advantage is not put to use in your example, 
  35. however.
  36.  
  37. The example you gave would be better rendered thusly:
  38.  
  39.     (searchItem*(this))->refCount++;
  40.  
  41. This form reduces the clutter and thus more clearly indicates 
  42. that you want this to point to its searchItem part and increment 
  43. the refCount data member.  However, if searchItem is a base 
  44. class of the class to which this points, and refCount is 
  45. (obviously) a protected or public member of searchItem (suspect 
  46. in itself, mind you), then you could just code this:
  47.  
  48.     refCount++;
  49.  
  50. The last form, I'm sure you'll agree is far more readable.
  51.  
  52. -- 
  53. Robert Stewart        | My opinions are usually my own.
  54. Datalytics, Inc.    | stew@datalytics.com
  55.